<?php
    include("../include/util.inc.php");
    /****************************
    *Function name: getSignup
    *Description: Store the new username and password in the database. 
                If the user name already exists, 
                the message "This username has existed" will be displayed.
    *Parameters: username; password
    *Return: message
    *******************************/
    function getSignup($username, $pwd){
        $pdo = getPDO();
        $pwd = $pdo->quote(password_hash($pwd,PASSWORD_DEFAULT));
        $username = $pdo->quote($username);
        $checksql = "SELECT * FROM user WHERE `username`=$username";
        $message = "null";
        if($pdo->query($checksql)->rowCount()>=1){
            $message = "This username has existed";
        }else{
            $sql = "INSERT INTO user(`username`,`password`) VALUES($username, $pwd)";
            $pdo->query($sql);
            $message = "success";
        }
        return $message;
    }
    /** 
    *Description: Receive the username and password 
    *submitted by js and call the function to store in the database.
    */
    try {
        $username = $_POST['username'];
        $pwd = $_POST['pwd'];
        echo getSignup($username, $pwd);
    } catch (\PDOException $e) {
        echo $e->getMessage();
    }
?>